﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Validater;

namespace COFE.Core.Filter
{
    /// <summary>
    /// Sort the output entries. 
    /// e.g. sort=size+ (Sort by size ascending.)
    /// e.g. sort=label- (Sort by label descending.)
    /// You can lookup part of the properties from \Entry\Properties\DefinedProperties.
    /// This is handled by individual list handler (e.g. AutoEntryList)
    /// </summary>
    [OptionFilter("sort")]
    public class SortResultOptionFilter : OptionFilterBase
    {
        public SortResultOptionFilter(string input, string id = "sort")
            : base(input, id)
        {
            SortPropertyString = input.TrimEnd('+', '-');
            SortProperty = COFEShared.PropertyDictionary.GetProperty(SortPropertyString);
            if (SortProperty == null)
                throw new ArgumentException(SortPropertyString + " is not a vaild property.");
            SortPropertyType = PropertyTypeExtension.GetDefaultPropertyType(SortProperty);
            SortDirection = SortDirectionType.Ascending;
            if (input.EndsWith("-"))
                SortDirection = SortDirectionType.Decending;
        }

        public SortDirectionType SortDirection { get; private set; }
        public string SortPropertyString { get; private set; }
        public object SortProperty { get; private set; }
        public Type SortPropertyType { get; private set; }      
    }
}
